package cn.lizemin.deleteFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class DeleteFile {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
        Timer timer = new Timer();
        Calendar calendar = Calendar.getInstance();
        timer.scheduleAtFixedRate(new MyTask(),calendar.getTime(),1000);

    }

}
class MyTask extends TimerTask{

    @Override
    public void run() {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(this.scheduledExecutionTime()));


        //删除文件
        /*File f = new File("C:\\Users\\15871\\Desktop\\mybatis\\lizemin.xls");
        if (f.exists()){
            f.delete();
        }*/

        //删除该目录下所有的文件
        File f2 = new File("C:\\Users\\15871\\Desktop\\mybatis\\test");
        if (f2.exists()){
            File[] files = f2.listFiles();
            for (File file : files) {
                //定时删除2个小时前的文件
                if (new Date().getTime() - file.lastModified() > 1000 * 60 * 60 * 2){
                    System.out.println(file.lastModified());
                    file.delete();
                }
            }
        }

    }


}
